package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增购物车商品
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //拷贝前端传过来的数据
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        log.info("查询购物车中是否存在商品");
        List<ShoppingCart> list=shoppingCartMapper.selectDishOrSetmeal(shoppingCart);
        // 判断加入物品在购物车中是否,存在,将数量加1
        if (list!=null && list.size()>0){
            ShoppingCart updateShoppingCart = list.get(0);
            updateShoppingCart.setNumber(updateShoppingCart.getNumber()+1);
            //更新数据
            log.info("购物车中已存在,数量加1");
            shoppingCartMapper.updateById(updateShoppingCart);
        }else {
            //不存在,添加数据
                //根据套餐/菜品查询对面名称,价格
            Long dishId = shoppingCart.getDishId();
            //菜品id不为空,说明是菜品
            if (dishId!=null){
                //查询菜品数据封装进类
                Dish dish = dishMapper.selectById(dishId);
                log.info("查询菜品数据:{}",dish);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());

            }else {
                //否则是套餐,查询套餐数据
                Long setmealId = shoppingCart.getSetmealId();
                Setmeal setmeal = setmealMapper.selectSetmealById(setmealId);
                log.info("查询套餐数据:{}",setmeal);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
//                shoppingCart.setNumber(1);
//                shoppingCart.setCreateTime(LocalDateTime.now());

            }
            //减少重复代码
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加进购物车
            shoppingCartMapper.insert(shoppingCart);
            log.info("添加购物车成功");

        }
    }

    /**
     * 根据用户id,查询购物车内的商品
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart>list=shoppingCartMapper.selectDishOrSetmeal(shoppingCart);
        return list;
    }

    /**
     * 根据id清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 购物车中商品数量减一
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //根据userid以及dishid/setmealid查询,只有一条数据
        List<ShoppingCart> list = shoppingCartMapper.selectDishOrSetmeal(shoppingCart);
        log.info("查询购物车结果:{}",list);
        if (list!=null && list.size()>0){
            //有且只有一条数据,为选中的商品
             shoppingCart = list.get(0);
            //根据商品数量进行操作
            Integer number = shoppingCart.getNumber();
            if (number==1){
                //只有一份,减一就是删除该商品
                log.info("商品数量为1,-1即将删除");
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                //数量不为1,只是单纯数量减少1
                log.info("商品数量 不为1,数量即将-1");
                shoppingCart.setNumber(shoppingCart.getNumber()-1);
                shoppingCartMapper.updateById(shoppingCart);
            }
        }
    }
}
